package com.reason.chat.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reason.chat.Config.TokenDecode;
import com.reason.chat.Mapper.FriendMapper;
import com.reason.chat.pojo.Afriend;
import com.reason.chat.pojo.Friend;
import com.reason.chat.pojo.Friendlist;
import com.reason.chat.pojo.Usermine;
import com.reason.chat.service.ChatService;
import com.reason.common.pojo.Result;
import com.reason.user.feign.CustomerFeign;
import com.reason.user.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 熊钒汐
 * @date 2020/7/2 11:21
 */
@Service
public class ChatServiceimpl implements ChatService {
//    @Autowired
//    TokenDecode tokenDecode;
    @Autowired
    CustomerFeign customerFeign;
    @Autowired
    FriendMapper friendMapper;

    @Override
    public List<Friendlist> getFriendes() {
        return null;
    }

    @Override
    public Usermine getUsermine(Long id) {
//        String phone = tokenDecode.getUserInfo().get("username");
        Result<Customer> result = customerFeign.findById(id);
        Customer customer = result.getData();
        Usermine usermine = new Usermine();
        usermine.setId(String.valueOf(customer.getId()));
        usermine.setAvatar(customer.getPhoto());
        usermine.setSign(customer.getPresonality());
        usermine.setUsername(customer.getNick());
        return usermine;
    }

    @Override
    public List<Friendlist> getFriendlist(Long id) {
        List<Usermine> usermineList = new ArrayList<>();
        Result<Customer> customerResult = customerFeign.findById(id);
        Customer customer = customerResult.getData();

        QueryWrapper<Friend> queryWrapper = new QueryWrapper<Friend>();
        queryWrapper.eq("userid", customer.getId());
        List<Friend> list = friendMapper.selectList(queryWrapper);
        for (Friend friend : list) {
            Result<Customer> result = customerFeign.findById(friend.getFriendid());
            customer = result.getData();
            Usermine usermine = new Usermine();
            usermine.setId(String.valueOf(customer.getId()));
            customer.getPhoto();
            List list1 = JSON.parseObject(customer.getPhoto(), List.class);
            usermine.setAvatar(String.valueOf(list1.get(0)));
            usermine.setSign(customer.getPresonality());
            usermine.setUsername(customer.getNick());
            usermineList.add(usermine);
        }
        Friendlist friendlist = new Friendlist();
        friendlist.setList(usermineList);

        List<Friendlist> friendlistList=new ArrayList<>();
        friendlistList.add(friendlist);
        return friendlistList;
    }

    @Override
    public Afriend getAfriend(Long uid, Long fid) {
        Afriend afriend = new Afriend();
        Result<Customer> result = customerFeign.findById(fid);
        Customer customer = result.getData();
        afriend.setId(String.valueOf(customer.getId()));
        afriend.setAvatar(customer.getPhoto());
        afriend.setUsername(customer.getNick());
        afriend.setSign(customer.getPresonality());
        return afriend;
    }

}
